package com.cloudkinto.controller.orderqoo;


import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.service.orderoutput.vo.OrderOutputAddReq;
import com.cloudkinto.service.orderqoo.vo.QooDownloadReq;
import com.cloudkinto.service.orderqoo.vo.QooExcelExportReq;
import com.cloudkinto.service.orderqoo.vo.QooSendOutReq;
import com.cloudkinto.service.orderqoo.vo.QooUpdateReq;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.cloudkinto.service.orderqoo.OrderQoo10Service;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cloudkinto.base.BaseController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhangyy
 * @since 2020-08-13
 */
@RestController
@Api(tags = "qoo模块")
@RequestMapping("/api/admin/qoo")
public class OrderQoo10Controller extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(OrderQoo10Controller.class);

    @Autowired
    private OrderQoo10Service service;

    @GetMapping("/addInit")
    @ApiOperation(value = "初始化", notes = "新增初始化")
    public SingleResult addInit() {
        return service.addInit(getCurrentUserId(), getCompanyId());
    }

    @UserSessionRequired
    @PostMapping("")
    @ApiOperation(value = "同步订单", notes = "同步订单")
    public SingleResult downloadQooOrders(@RequestBody QooDownloadReq req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        service.downloadQooOrders(req, userId, companyId);
        return SingleResult.success();
    }

    @PostMapping("sendOut")
    @ApiOperation(value = "发货", notes = "同步订单")
    public SingleResult sendOut(@RequestBody QooSendOutReq req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return service.sendOut(req, userId, companyId);
    }

    @UserSessionRequired
    @PostMapping("sendOutMatch")
    @ApiOperation(value = "确定发货", notes = "确定发货")
    public SingleResult sendOutMatch(@RequestBody List<OrderOutputAddReq> orderOutputAddReqList) {
        Long companyId = getCompanyId();
        Long userId = getCurrentUserId();
        return service.sendOutMatch(orderOutputAddReqList, userId, companyId);
    }

    @UserSessionRequired
    @PostMapping("markSendOut")
    @ApiOperation(value = "标记发货", notes = "同步订单")
    public SingleResult markSendOut(@RequestBody List<Long> ids) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        service.markSendOut(ids, userId, companyId);
        return SingleResult.success();
    }

    @GetMapping("/sysMemo/{id}")
    @ApiOperation(value = "编辑初始化", notes = "编辑初始化")
    public SingleResult updateInit(@PathVariable("id") Long id) {
        return service.updateInit(id);
    }

    @PutMapping("sysMemo")
    @ApiOperation(value = "编辑备注", notes = "修改")
    public SingleResult update(@RequestBody @Validated QooUpdateReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", service.update(req, userId));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功", service.delete(id, userId));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", service.detail(id));
    }

    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit(getCurrentUserId(), getCompanyId());
    }

//    @GetMapping("/list")
//    @ApiOperation(value = "查询", notes = "查询")
//    public SingleResult list(HttpServletRequest request){
//        Long companyId = getCompanyId();
//
//        Map<String, String> map = new HashMap<>();
//        String keyword = request.getParameter("keyword");
//        map.put("keyword", keyword);
//        return SingleResult.success("查询成功", service.getList(map));
//    }

    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "keyword", value = "关键字（模糊查询）"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "startDate", value = "开始时间"),
            @ApiImplicitParam(name = "ShippingStat", value = "运输状态"),
            @ApiImplicitParam(name = "endDate", value = "结束时间"),
            @ApiImplicitParam(name = "isSendOut", value = "是否发出"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")})
    public PageResult listPage(HttpServletRequest request) {
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        String current = request.getParameter("current");
        String startDate = request.getParameter("startDate");
        String ShippingStat = request.getParameter("ShippingStat");
        String endDate = request.getParameter("endDate");
        String isSendOut = request.getParameter("isSendOut");
        String size = request.getParameter("pageSize");
        map.put("keyword", keyword);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("ShippingStat", ShippingStat);
        map.put("isSendOut", isSendOut);
        map.put("current", current);
        map.put("pageSize", size);

        if (companyId != null) {
            map.put("companyId", companyId + "");
        }
        return service.getListPage(map);
    }

    @UserSessionRequired
    @PostMapping("exportExcel")
    @ApiOperation(value = "导出所有数据excel")
    public void exportAll(@RequestBody QooExcelExportReq req, HttpServletResponse response) throws Exception {
        Long companyId = getCompanyId();
        Map<String, String> map = new HashMap<>();
        String keyword = req.getKeyword();
        String startDate = req.getStartTime();
        String ShippingStat = req.getShippingStat();
        String endDate = req.getEndTime();
        map.put("keyword", keyword);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("ShippingStat", ShippingStat);

        if (companyId != null) {
            map.put("companyId", companyId + "");
        }
        service.exportExcel(map, req.getIds(), response);
    }

}

